package edu.ccut.saturn.workflow.startWorkFlowModule.code;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.ComponentFactory;
import edu.ccut.saturn.component.ComponentManager;
import edu.ccut.saturn.component.IComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.WorkFlowTools;
import edu.ccut.saturn.workflow.model.ActivityInfo;
import edu.ccut.saturn.workflow.model.LogicKeyInfo;
import edu.ccut.saturn.workflow.model.ParameterInfo;
import edu.ccut.saturn.workflow.model.WorkFlowInfo;

public class ActivateAutoActivity implements IActivateActivity {

	@SuppressWarnings("unchecked")
	public void activate(IParameterContext parameterContext) throws Exception {
		// 获取参数
		SaturnData workFlowInstance = (SaturnData) parameterContext
				.getParameterValue(0);
		WorkFlowInfo workFlowInfo = (WorkFlowInfo) parameterContext
		.getParameterValue(1);
		// previousActivityInstance 启动时：开始活动节点；完成时：完成活动实例
		SaturnData previousActivityInstance = (SaturnData) parameterContext
				.getParameterValue(3);
		Map<String, String> customParamMap = (Map<String, String>) parameterContext
				.getParameterValue(4);
		// 当前活动定义对象
		ActivityInfo currentActivityInfo = (ActivityInfo) parameterContext
				.getParameterValue(5);
		//创建临时上下文
		IParameterContext tempParameterContext = ComponentFactory.createParameterContext();
		tempParameterContext.addParameter(workFlowInstance.get("process_instance_id")+"", String.class);
		tempParameterContext.addParameter(null, HashMap.class);
		//执行获得数据库中参数组件
		IComponent getFormDataKeyComponent = ComponentManager.getInstance().getComponent("edu.ccut.saturn.workflow::/parseWorkFlowModule/logic/GetFormDataValue.logic");
		getFormDataKeyComponent.execute(tempParameterContext);
		//获得参数
		HashMap<String, String> formDataValueMap = (HashMap<String, String>)tempParameterContext.getParameterValue(1);
		//执行扩展逻辑
		List<LogicKeyInfo> logicKeyList = currentActivityInfo.getLogicKey();
		if (logicKeyList != null && logicKeyList.size() > 0) {
			for(LogicKeyInfo logic : logicKeyList){
				tempParameterContext = ComponentFactory.createParameterContext();
				List<ParameterInfo> paramList = logic.getParamList();
				for (ParameterInfo paramInfo : paramList) {
					String value = (String) customParamMap.get(paramInfo.getKey());
					// 如果当前Map没有，就到数据库中查，
					if (value == null)
						value = (String) formDataValueMap.get(paramInfo
								.getKey());

					tempParameterContext.addParameter(value, String.class);
				}
				
				IComponent logicComponent = ComponentManager.getInstance().getComponent(logic.getFullKey());
				logicComponent.execute(tempParameterContext);
			}
		}
		
		Connection conn = parameterContext.getConnection();
		// 获得当前活动数据项并附值
		SaturnData currentActivityInstance = WorkFlowTools.getActivityInstance(
				workFlowInstance, currentActivityInfo, conn);
		// 核心操作＝＝＝＝插入自动活动实例
		Operation.dataBase().insert(currentActivityInstance,
				parameterContext.getConnection());
		// 核心操作＝＝＝＝插入流转实例
		SaturnData transitionInstance = WorkFlowTools.getTransitionInstance(
				conn, previousActivityInstance, currentActivityInstance);
		Operation.dataBase().insert(transitionInstance, conn);
		
		parameterContext.setParameter(3, currentActivityInstance);//将当前活动替换为下一活动的前一活动
		parameterContext.setParameter(5, workFlowInfo.getActivityInfoById(currentActivityInfo.getNext()));
		WorkFlowTools.checkToActivateActivity(parameterContext);
		
	}

}
